package com.LearnInWorld.video.service.impl;

import com.LearnInWorld.video.mapper.ModuleMapper;
import com.LearnInWorld.video.pojo.Module;
import com.LearnInWorld.video.service.ModuleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ModuleServiceImpl implements ModuleService {
    @Autowired
    private ModuleMapper moduleMapper;

    /***
     * 查询所有
     * @return
     */
    @Override
    public List<Module> findAll() {
        List<Module> moduleList = moduleMapper.selectAll();
        return moduleList;
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Module findById(Long id) {
        Module module = moduleMapper.selectByPrimaryKey(id);
        return module;
    }

    /***
     * 新增
     * @param module
     */
    @Override
    public void add(Module module) {
        moduleMapper.insertSelective(module);
    }

    @Override
    public void update(Module module) {
        moduleMapper.updateByPrimaryKey(module);
    }

    /***
     * 修改
     * @param module
     */
    @Override
    public void update(Module module, Long Id) {
        module.setModuleId(Id);
        moduleMapper.updateByPrimaryKey(module);
    }

    /***
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        moduleMapper.deleteByPrimaryKey(id);
    }

    /***
     * 多条件搜索
     * @param searchMap
     * @return
     */
    @Override
    public List<Module> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return moduleMapper.selectByExample(example);
    }


    /***
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Module> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Module>) moduleMapper.selectAll();
    }

    /***
     * 多条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Module> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        return (Page<Module>) moduleMapper.selectByExample(example);
    }

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Module.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // id
            if (searchMap.get("tbId") != null && !"".equals(searchMap.get("tbId"))) {
                criteria.andLike("tbId", "%" + searchMap.get("tbId") + "%");
            }
            // 模块name
            if (searchMap.get("moduleName") != null && !"".equals(searchMap.get("moduleName"))) {
                criteria.andLike("moduleName", "%" + searchMap.get("moduleName") + "%");
            }
            // 模块id
            if (searchMap.get("moduleId") != null && !"".equals(searchMap.get("moduleId"))) {
                criteria.andLike("moduleId", "%" + searchMap.get("moduleId  ") + "%");
            }
        }
        return example;
    }

}
